package com.wanqing.community.filter;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

/**
 * 
 * <p>Description:产生验证码<p>
 * All right reserved.
 */
public class CaptchaSevlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	private static Logger log = Logger.getLogger(CaptchaSevlet.class);


	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		doPost(request,response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		CodeKey(request,response);
	}


	/**
	 * 
	  * <p>Description:产生图片并保存到session里面<p>
	  * @modifier 
	  * @modifiDate
	  * <p>modifiContent:<p>
	  * @param request
	  * @param response
	 */
	public void CodeKey(HttpServletRequest request,HttpServletResponse response){
		int x=14; 
		int fontHeight=18; 
		int codeY=16; 
		int width=60; 
		int height=20; 
		int codeNum=4; 
		char[] codes = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' 
				,'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
				'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
				'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w','x', 'y', 'z'}; 
		BufferedImage buffImg = new BufferedImage(
				width, height,BufferedImage.TYPE_INT_RGB); 
				
				Graphics2D g = buffImg.createGraphics();  
				
				g.setColor(Color.WHITE); 
				g.fillRect(0, 0, width, height); 
				
				Font font = new Font("Fixedsys", Font.PLAIN, fontHeight); 
				
				g.setFont(font); 
				
				g.setColor(Color.BLACK); 
				g.drawRect(0, 0, width - 1, height - 1); 
				Random random = new Random();
				StringBuffer randomCode = new StringBuffer(); 
				int red = 0, green = 0, blue = 0; 
				for (int i = 0; i < codeNum; i++) {
				String strRand = String.valueOf(codes[random.nextInt(codes.length)]); 
				red = random.nextInt(255); 
				green = random.nextInt(255); 
				blue = random.nextInt(255); 

				g.setColor(new Color(red, green, blue)); 
				g.drawString(strRand, 5+i*x, codeY); 

				randomCode.append(strRand); 
				}
				HttpSession session = request.getSession(); 
				//讲验证码保存到session里面
				session.setAttribute("CodeKey", randomCode.toString().toLowerCase()); 

				//输出图片
				response.setHeader("Pragma", "no-cache"); 
				response.setHeader("Cache-Control", "no-cache"); 
				response.setDateHeader("Expires", 0); 
				response.setContentType("image/jpeg"); 
				ServletOutputStream sos;
				try {
					sos = response.getOutputStream();
					ImageIO.write(buffImg, "jpeg", sos); 
					sos.close(); 
				} catch (IOException e) {
					log.error("产生验证码错误："+e.toString());
				} 

	}
}
